<template>
    <table border="1">
        <tr>
            <td colspan="2" style="text-align:center;font-size:30px;">房号管理-添加</td>
        </tr>
        <tr>
            <td>房号名称</td>
            <td>
                <input type="text" v-model="Num.Rnum_Name"/>
            </td>
        </tr>
        <tr>
            <td>房型</td>
            <td>
                <select v-model="Num.Rtype_Id">
                    <option :value="0">-请选择---</option>
                    <option v-for="item in Info" :value="item.Rtype_Id">{{item.Rtype_Name}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>房态</td>
            <td>
                <input type="radio" :value="true" v-model="Num.Rnum_State"/>上架
                <input type="radio" :value="false" v-model="Num.Rnum_State"/>下架
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center;">
                <input type="button" value="添加" @click="addRoomNumber"/>
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import {useRouter,useRoute} from 'vue-router'

//创建路由实例
const router=useRouter();
const route=useRoute();

//创建生命周期钩子
onMounted(()=>{
    GetRoomType();
})

//创建响应式数据
const Info=ref([{
    Rtype_Id:0,
    Rtype_Name:""
}])

//显示下拉框
const GetRoomType=()=>{
    axios.get("https://localhost:7249/api/RoomNumber/GetRoomType")
    .then(res=>{
        Info.value=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
    .catch(err=>{
        console.log(err);
    })
}

//创建响应式数据
const Num=ref({
   Rnum_Id:0,
   Rnum_Name:"",
   Rnum_State:true,
   Rtype_Id:0,
   Rtype_Name:""
})

//保存数据
const addRoomNumber=()=>{

    if(Num.value.Rnum_Name=="")
    {
        alert("房号名称不能为空");
        return;
    }

    axios.post("https://localhost:7249/api/RoomNumber/AddRoomNumber",Num.value)
    .then(res=>{
        if(res.data==-1)
        {
            alert("当前房号名称已存在");
            return;
        }
        else if(res.data>0)
        {
            alert("添加成功");
            router.push("/ShowRoomNumber");
        }
        else{
            alert("添加失败");
        }
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>